﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UFPC.Models;

namespace UFPC.Controllers
{
    [Authorize]
    public class CustomerController : Controller
    {
        private IUfpcRepository _repository;

        public CustomerController() : this(new UfpcRepository())
        { 
            
        }

        public CustomerController(IUfpcRepository repository)
        {
            _repository = repository;
        }
        
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View(_repository.ListCustomers());
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            /* CustomerViewModel customerViewModel = new CustomerViewModel();
            customerViewModel.Customer = _repository.CustomerDetails(id);
            customerViewModel.CustomerProductPrice = _repository.GetProductsByCustomer(id);*/
            
            return View(_repository.CustomerDetails(id));
        }

        public ActionResult AddProduct(int id)
        {
            CustomerViewModel customerProduct = new CustomerViewModel();
            CustomerProductPrice customerPrice = new CustomerProductPrice();

            customerPrice.CustomerId = id;
            customerProduct.CustomerPrice = customerPrice;
            customerProduct.ProductList = new SelectList(_repository.ListProducts(), "ProductId", "Name");
            
            return View(customerProduct);
        }

        [HttpPost]
        public ActionResult AddProduct(CustomerViewModel newCustomerProduct)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.AddProductToCustomer(newCustomerProduct.CustomerPrice);
                    _repository.Save();

                    return RedirectToAction("Details", new { id = newCustomerProduct.CustomerPrice.CustomerId });
                }
                else
                {
                    newCustomerProduct.ProductList = new SelectList(_repository.ListProducts(), "ProductId", "Name");

                    return View(newCustomerProduct);
                }
            }
            catch
            {
                ModelState.AddModelError(string.Empty, "Product already exists for customer.");
                newCustomerProduct.ProductList = new SelectList(_repository.ListProducts(), "ProductId", "Name");

                return View(newCustomerProduct);
            }           
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude="CustomerId,AddressId")]Customer newCustomer)
        {
            if (ModelState.IsValid)
            {
                _repository.AddCustomer(newCustomer);
                _repository.Save();

                return RedirectToAction("Index");
            }
                
            return View();
        }
        
        //
        // GET: /Customer/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_repository.CustomerDetails(id));
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Customer customerDetails = _repository.CustomerDetails(id);

            try
            {
                UpdateModel(customerDetails);
                _repository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult EditProduct(int customerId, int productId)
        {
            CustomerViewModel customerProduct = new CustomerViewModel(_repository.CustomerProduct(customerId, productId), _repository.ListProducts().ToList());

            return View(customerProduct);
        }

        [HttpPost]
        public ActionResult EditProduct(int customerId, int productId, FormCollection collection)
        {
            CustomerViewModel customerProduct = new CustomerViewModel(_repository.CustomerProduct(customerId, productId), _repository.ListProducts().ToList());

            try
            {
                TryUpdateModel(customerProduct);
                _repository.Save();

                return RedirectToAction("Details", new { id = customerId});
            }
            catch
            {
                throw;
                //return View(customerProduct);
            }
            
        }

        //
        // GET: /Customer/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_repository.CustomerDetails(id));
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Customer customer = _repository.CustomerDetails(id);

            _repository.DeleteCustomer(customer);
            _repository.Save();

            return RedirectToAction("Index");
        }

        
        public ActionResult DeleteProduct(int customerId, int productId, FormCollection collection)
        {
            CustomerViewModel customerProduct = new CustomerViewModel(_repository.CustomerProduct(customerId, productId), _repository.ListProducts().ToList());

            _repository.DeleteProduct(customerProduct.CustomerPrice);
            _repository.Save();
            
            return RedirectToAction("Details", new { id = customerId });
        }
    }
}
